import yaml
import os
import time
from werkzeug.datastructures import FileStorage


with open('config.yml', 'r') as f:
    config = yaml.safe_load(f)


# 保存上传视频
def upload_video(input_file: FileStorage):
    input_dir = config['input_video_dir']
    if not os.path.exists(input_dir):
        os.makedirs(input_dir)
    # 文件以时间戳命名保存
    file_extension = os.path.splitext(input_file.filename)[1]
    timestamp_str = str(int(time.time()))
    new_file_name = timestamp_str + file_extension
    input_video_path = input_dir + new_file_name
    input_file.save(input_video_path)

    return new_file_name


def get_config_output_video_dir():
    return config['output_video_dir']

# 获得视频对应csv表格名称
def output_csv(file_name):
    file_name_map = os.path.splitext(file_name)
    output_dir = config['output_video_dir']
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    output_csv = output_dir + file_name_map[0]+".csv"
    print(output_csv)
    return output_csv
